package cn.com.connext.scrm.repository.master;

import cn.com.connext.scrm.domain.master.Comment;
import cn.com.connext.scrm.domain.master.Note;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by YANG205 on 2018/6/27.
 */
@Repository
public interface CommentRepository extends JpaRepository<Comment, String>, JpaSpecificationExecutor {

    //查询某帖子所有评论数量(一级评论)
    @Query(value = "select count(a) from Comment a where a.noteId = :noteId and a.replyType = 0 and a.isDelete = false ")
    Integer countAllByNoteId(@Param("noteId") String noteId);

    //查询某帖子所有一级评论
    @Query(value = "select a from Comment a where a.noteId = :noteId and a.replyType = 0 and a.isDelete = false order by a.createTime asc ")
    List<Comment> findByNoteId(@Param("noteId") String noteId);

    //查询某个一级评论下所有二级评论数量
    @Query(value = "select count(a) from Comment a where a.commentId = :Id and a.replyType = 1 and a.isDelete = false")
    Integer countTotalReplay(@Param("Id") String Id);

    //查询某个一级评论下所有二级评论
    @Query(value = "select a from Comment a where a.commentId = :Id and a.replyType = 1 and a.isDelete = false order by a.createTime")
    List<Comment> findAllReply(@Param("Id") String Id);

    @Query(value = "select note_id as noteId,count(0) as counts from comment where reply_type = 0 and is_delete = 0 group by note_id", nativeQuery = true)
    List<Object[]> countAll();

    //查询当月
    @Query(value = "select a.* from comment a where user_id= :userId AND date_format(create_time,'%Y-%m')=date_format(now(),'%Y-%m') ", nativeQuery = true)
    List<Comment> findCommentByUserIdAndMonth(@Param("userId") String userId);

    //查询当天
    @Query(value = "select a.* from comment a where  user_id= :userId AND to_days(create_time) =TO_DAYS(NOW()) ", nativeQuery = true)
    List<Comment> findCommentByUserIdAndDay(@Param("userId") String userId);
}
